package com.cheng.io.input;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Test:FileInputStream
 *      int inputStream.read(byte[] bytes); 一次读取byte数组大小个字节并存储在数组中，返回值为读取的字节数量
 */
public class FileInputStreamTest02 {
    public static void main(String[] args) {
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("D:\\code\\java-study\\java-study\\io\\resources\\test01.txt");

            byte[] bytes = new byte[4];
            int read = 0;
            /*System.out.println(read);

            read = inputStream.read(bytes);
            System.out.println(read);

            read = inputStream.read(bytes);
            System.out.println(read);*/

            //改进
            while ((read = inputStream.read(bytes)) != -1) {
                //System.out.print(new String(bytes,0,4));
                System.out.println(new String(bytes,0,read));
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
